const WXAPI = require('apifm-wxapi')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stockId: 0,
    fruitName: '',
    pics:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      fruitName : options.fruitName,
      sellRequestId : options.sellRequestId
    })                                                                                                                                                                                                                                                                                                                                                                                                                         
    
    console.log("代卖页面收到数据："+this.data.sellRequestId+" - "+this.data.fruitName)
  },

  

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  bindCancel: function () {
    wx.navigateBack({})
  },

  // 申请代卖
  async bindSave(e) {
    var fruitSellRequestId = this.data.sellRequestId;
    var fruitName = this.data.fruitName;

    var requestData = {
      'sellRequestId':fruitSellRequestId,
      'fruitName': fruitName,
      'shelfName':e.detail.value.shelfName,
      'shelfDesc':e.detail.value.shelfDesc,
      'pic': this.data.pics
    }

    console.log("代卖对象："+JSON.stringify(requestData))
    
    console.log("同意代卖，代卖Id："+fruitSellRequestId)
    const res = await WXAPI.confirmSellRequest(requestData);
    console.log("代卖结果："+JSON.stringify(res))
    if(res.code == 20000){
      wx.showModal({
        title: '成功',
        content: '代卖成功',
        showCancel: false
      })
      this.getSellRequestList();
    }else{
      wx.showModal({
        title: '失败',
        content: '代卖失败',
        showCancel: false
      })
    }
    
  },
  updateHead: function () {
    var that = this;
      wx.showActionSheet({
      itemList: ['从相册选择','拍照'],
      itemColor:"#f7982a",
      success:function(res){
      if(!res.cancer){
      if (res.tapIndex==0){
      that.chooseWxImageShop('album');
      } else if (res.tapIndex == 1) {
      that.chooseWxImageShop('camera')
      }
      }

      }
    })
  },

  chooseWxImageShop: function (type) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        // 上传多张（遍历数组，一次传一张）
        for (var index in res.tempFilePaths) {
          that.upload_file('http://localhost:8082/fruit/sell/request/uploadFile', res.tempFilePaths[index])
        }
      }
    })
  },

  upload_file: function (url, filePath) {
    var that = this;
    wx.uploadFile({
    url: url,
    filePath: filePath,
    name: 'file',
    header: {
      'content-type': 'multipart/form-data'
    }, // 设置请求的 header
    formData: { 'guid':"procomment" }, // HTTP 请求中其他额外的 form data
    success: function (res) {
      console.log(res.data+ that.data.pics)
      if(''== that.data.pics){
        var pic = that.data.pics + res.data
      }else{
        var pic = that.data.pics+";"+res.data
      }
      that.setData({
        pics: pic
      })
      console.log("图片路径："+that.data.pics)
    },
    fail: function (res) {
    }
    })
  },
})